//
//  LoginViewController.m
//  TaxDealPlatform
//
//  Created by Zhengtong on 15/5/5.
//  Copyright (c) 2015年 Zhengtong. All rights reserved.
//

#import "LoginViewController.h"
#import "DefineHeader.h"
#import "NSObject+ToolObject.h"

#import "ZTSiginViewController.h"
#import "ZTTabBarController.h"

#import <AdSupport/AdSupport.h>

#import "ZTForgetPswController.h"

#import "ZTSiginWebViewController.h"
#import "APService.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *topImageView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollerView;
@property (weak, nonatomic) IBOutlet UITextField *userNameField;
@property (weak, nonatomic) IBOutlet UITextField *passerWordField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
- (IBAction)loginBtnClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIView *myView;

@property (weak, nonatomic) IBOutlet UIButton *pwdBtn;


- (IBAction)forgetBtnClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;

@end

@implementation LoginViewController

-(void)dealloc
{
    [self removeNotification];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self removeNotification];
    [self addKeyBoardNotification];
    
    
    self.scrollerView.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);
    
    UIImage *img = [UIImage imageNamed:@"logo"];
    if (DEVICE_HEIGHT > 480) {
        self.topImageView.frame = CGRectMake((DEVICE_WIDTH - img.size.width)*0.5, 70, img.size.width, img.size.height);
        
    }else{
        self.topImageView.frame  = CGRectMake((DEVICE_WIDTH - 80)*0.5, 70, 80, 85);
    }
    self.myView.frame = CGRectMake(0, CGRectGetMaxY(self.topImageView.frame)+20, DEVICE_WIDTH, 104);
    
    self.userNameField.frame = CGRectMake(112, self.userNameField.y, DEVICE_WIDTH-120, 30);
    
    self.passerWordField.frame = CGRectMake(112, 65, DEVICE_WIDTH-120, 30);
    
    self.loginBtn.frame = CGRectMake(30, CGRectGetMaxY(self.myView.frame)+40, DEVICE_WIDTH - 60, 44);
    
    self.pwdBtn.frame = CGRectMake(DEVICE_WIDTH-105, CGRectGetMaxY(self.loginBtn.frame)+10, 90, 40);
    
    self.label1.frame = CGRectMake(0, self.label1.y, DEVICE_WIDTH, 1);
    self.label2.frame = CGRectMake(0, self.label2.y, DEVICE_WIDTH, 1);
    
 
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}
- (IBAction)loginBtnClick:(UIButton *)sender {

    if (![self judgmentIsEmpty:self.userNameField.text]) {
        [self showHUD:@"用户名不能为空!"];
    }else if (![self judgmentIsEmpty:self.passerWordField.text])
    {
        [self showHUD:@"密码不能为空!"];
    }
    else{
        [self showActivite:@"正在登录..."];
        
        [RequestTool requestLoginName:self.userNameField.text
                         withPassword:self.passerWordField.text
                         withClientOS:CLIENTDEVICEOS
                         withClientId:DEVICEID
                       withDeviceName:[UIDevice currentDevice].model
                            WithBlock:^(id dict,NSString *error) {
                                if (error==nil&&dict !=nil) {
                                     [self.MBhud hide:YES];
                                    [APService setAlias:[dict objectForKey:@"userId"] callbackSelector:nil object:nil];
                                    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
                                    
                                    [userDefault setObject:@"YES" forKey:@"isFirstLogin"];
                                    
                                    [userDefault setObject:self.userNameField.text       forKey:USERLOGINNAME];
                                    [userDefault setObject:self.passerWordField.text     forKey:USERLOGINPASSORD];
                                    [userDefault setObject:[dict objectForKey:@"userId"] forKey:USERID];
                                    [userDefault setObject:[dict objectForKey:DECLAREDATEVERSION] forKey:DECLAREDATEVERSION];
                                    [userDefault setObject:[dict objectForKey:@"orgShortCode"] forKey:@"orgShortCode"];
                                    [userDefault setObject:[dict objectForKey:@"pwd"]   forKey:@"pwd"];
                                    [userDefault setObject:self.userNameField.text      forKey:@"code"];
                                    [userDefault setObject:[dict objectForKey:@"orgId"] forKey:@"orgId"];
                                    [userDefault setObject:[dict objectForKey:@"email"] forKey:@"email"];
                                    [userDefault setObject:[dict objectForKey:@"pwd"]   forKey:@"pwd"];
                                    [userDefault setObject:[dict objectForKey:@"userType"] forKey:@"userType"];
                                    [userDefault setObject:[dict objectForKey:@"damgtUrl"] forKey:@"damgtUrl"];//报表前缀
                                    [userDefault setObject:[dict objectForKey:@"mailUrl"] forKey:@"mailUrl"];  //邮箱前缀
                                    [userDefault synchronize];
                                    
                                    if (![[dict objectForKey:@"orgShortCode"] isEqualToString:@"26104"]) {
                                        UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
                                        ZTTabBarController *tab = [[ZTTabBarController alloc] init];
                                        window.rootViewController = tab;
                                    }else{
                                        [self showHUD:@"用户名或密码错误"];
                                    }
 
                                }else
                                {
                                    [self showHUD:error];
                                }
                                
                                
        }];
   
    }
}

- (IBAction)forgetBtnClick:(UIButton *)sender {
    
    ZTForgetPswController *forget = [[ZTForgetPswController alloc] init];
    forget.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:forget animated:YES];

}

#pragma mark  - textFieldDelegate -
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.userNameField) {
        [self.passerWordField resignFirstResponder];
       
    }else if (textField == self.passerWordField)
    {
        [self.userNameField resignFirstResponder];
        
    }
    [self.scrollerView setContentOffset:CGPointMake(0, 100) animated:YES];
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    [self.scrollerView setContentOffset:CGPointMake(0, 0) animated:YES];
    return YES;
}
-(void)keyBoarHide
{
    [self.passerWordField resignFirstResponder];
    [self.userNameField resignFirstResponder];
    [self.scrollerView setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

@end
